package io.stream;


import java.io.*;
import java.net.URL;

public class TestInputoutputstream {
    public  static  void  main(String []args) throws IOException {

        //copyAndWrite1();
        copyAndWrite2();
        //copyAndWrite3();
        //copyAndWrite4();
    }

    /**
     * fileinputstream read one byte
     * @throws IOException
     */
    private static void copyAndWrite1() throws IOException {

        URL tempURL= TestInputoutputstream.class.getClassLoader().getResource("a/a.txt");
        String tempString=tempURL.getFile();
        FileInputStream fis=new FileInputStream(tempString);
        FileOutputStream fos=new FileOutputStream("b.txt");
        int byt=0;
        while ((byt=fis.read())!=-1)
        {
            System.out.print((char)byt);
            fos.write(byt);
        }

        fis.close();
        fos.close();
    }

    /**
     * fileinputstream read 1024 byte
     * @throws IOException
     */
    private static void copyAndWrite2() throws IOException {
        URL tempURL= TestInputoutputstream.class.getClassLoader().getResource("a/a.txt");
        String tempString=tempURL.getFile();
        FileInputStream fileInputStream=new FileInputStream(tempString);
        FileOutputStream fileOutputStream=new FileOutputStream("b.txt");
        byte[] bytes=new byte[1024];
        int len=0;
        while ((len=fileInputStream.read(bytes))!=-1)
        {
            System.out.print(new String(bytes,"utf-8"));
            fileOutputStream.write(bytes,0,len);
        }

        fileInputStream.close();
        fileOutputStream.close();

    }

    /**
     * bufferedinputstream read one byte
     * @throws IOException
     */
    private static void copyAndWrite3() throws IOException {
        BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("rs/a.txt"));
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("b.txt"));
        int byt;
        while ((byt=bufferedInputStream.read())!=-1)
        {
            System.out.print((char)byt);
            bufferedOutputStream.write(byt);
        }

        bufferedInputStream.close();
        bufferedOutputStream.close();
    }

    /**
     * bufferedinputstream 1024 byte
     * @throws IOException
     */
    private static void copyAndWrite4() throws IOException {
        BufferedInputStream bufferedInputStream =new BufferedInputStream(new FileInputStream("rs/a.txt"));
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("b.txt"));

        byte[] bytes=new byte[1024];
        int len=0;
        while ((len=bufferedInputStream.read(bytes))!=-1)
        {
            System.out.print(new String(bytes));
            bufferedOutputStream.write(bytes,0,len);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();

    }



}
